//
//  LiveListVC.m
//  EducationLive
//
//  Created by Apple on 2017/9/29.
//  Copyright © 2017年 ZhouHao. All rights reserved.
//
//判断Cell类型
#define CateType(section) [[self dataParse:section] isEqualToString:@"cate"]
#define AdvertType(section)  [[self dataParse:section] isEqualToString:@"advert"]
#define GroupType(section)  [[self dataParse:section] isEqualToString:@"group"]

#import "LiveListVC.h"
#import "LiveCellNode.h"
#import "LiveHeaderNode.h"
#import "LiveFooterNode.h"

@interface LiveListVC ()<ASCollectionDataSource, ASCollectionDelegate, ASCollectionDelegateFlowLayout>
// UI
@property (nonatomic, strong) ASCollectionNode *collectionNode;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
// Data

@end

#define kRecommendPagerHeight 113
#define kRecommendItemWidth 48
#define kRecommendItemHorEdge (kScreenWidth > 320 ? 40 : 10)
#define kRecommendItemVerEdge 10

@implementation LiveListVC

#pragma mark - life cycle
- (instancetype)init
{
    // 加载Collection
    [self addCollectionNode];
    
    self = [super initWithNode:_collectionNode];
    if (self) {
        _collectionNode.delegate = self;
        _collectionNode.dataSource = self;
    }
    return self;
}

// 加载Collection
- (void)addCollectionNode
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _collectionNode = [[ASCollectionNode alloc] initWithCollectionViewLayout:flowLayout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 加载注册头尾视图
    [self configureCollection];
    // 加载下拉刷新
    [self loadHeadRefresh];
    // 加载数据
    [self loadData];
}

#pragma mark - load refresh
// 加载下拉刷新
- (void)loadHeadRefresh
{
    __weak __typeof(self) weakSelf = self;
    // 下拉刷新
    _collectionNode.view.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 结束刷新
        [weakSelf.collectionNode.view.mj_header endRefreshing];
    }];
}

#pragma mark - load data
// 加载数据
- (void)loadData
{
    __typeof (self) __weak weakSelf = self;
    //    [NetworkState networkState:^(bool has) {
    // 栏目接口
    NSDictionary *info = @{@"cate_id":_cate_id};
    [MAAFRequest getIndex:info networkState:NO successBlock:^(id responseBody) {
        //获取数据
        LiveListModel *liveListModel = [[LiveListModel alloc] initWithDictionary:Infos error:nil];
        weakSelf.liveListModel = liveListModel;
        // 刷新数据
        [weakSelf.collectionNode reloadData];
    } failureBlock:^(NSString *error) {

    }];
//            }];
}
#pragma mark - configure
// 加载注册头尾视图
- (void)configureCollection
{
    //防止tabbar遮挡
    if (_extendedTabBarInset) {
        _collectionNode.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
//        _collectionNode.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 49, 0);
    }
    
    [_collectionNode registerSupplementaryNodeOfKind:UICollectionElementKindSectionHeader];
    [_collectionNode registerSupplementaryNodeOfKind:UICollectionElementKindSectionFooter];
}

#pragma mark - ASCollectionDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    NSInteger sectionCount = 0;
    if (_liveListModel.cate.count > 0) {
        sectionCount++;
    }
    if (_liveListModel.advert.count > 0){
        sectionCount++;
    }
    if (_liveListModel.group.count > 0){
        sectionCount = sectionCount + _liveListModel.group.count;
    }
    return sectionCount;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (CateType(section)) {
        return _liveListModel.cate.count;
    }else if (AdvertType(section)) {
        return _liveListModel.advert.count;
    }else if (GroupType(section)) {
        if (_liveListModel.cate.count > 0 && _liveListModel.advert.count > 0 && _liveListModel.group.count > 0) {
            GroupModel *groupModel = _liveListModel.group[section - 2];
            return groupModel.arr.count;
        }else if ((_liveListModel.cate.count > 0  && _liveListModel.group.count > 0) || (_liveListModel.advert.count > 0  && _liveListModel.group.count > 0)) {
            GroupModel *groupModel = _liveListModel.group[section - 1];
            return groupModel.arr.count;
        }else{
            GroupModel *groupModel = _liveListModel.group[section];
            return groupModel.arr.count;
        }
    }else{
        return 0;
    }
}

- (ASCellNodeBlock)collectionView:(ASCollectionView *)collectionView nodeBlockForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ASCellNode *(^cellNodeBlock)() = ^ASCellNode *() {
        ASCellNode *cellNode = nil;
        if (CateType(indexPath.section)) {
            cellNode = [[LiveCellNode alloc]initWithItem:_liveListModel.cate[indexPath.row]];
        }else if (AdvertType(indexPath.section)) {
            cellNode = [[LiveAdCellNode alloc]initWithItem:_liveListModel.advert[indexPath.row]];
        }else if (GroupType(indexPath.section)) {
            cellNode = [[LiveRecommendCellNode alloc]initWithItem];
        }else{
            cellNode = [[ASCellNode alloc]init];
        }
        return cellNode;
    };
    return cellNodeBlock;
}
//设置单个元素的边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (CateType(section)) {
        return UIEdgeInsetsMake(15, kRecommendItemHorEdge, kRecommendItemVerEdge, kRecommendItemHorEdge);
    }else if (GroupType(section)) {
        return UIEdgeInsetsMake(0, 10, 10, 10);
    }else{
        return UIEdgeInsetsZero;
    }
}
//设置每行上下距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    if (CateType(section)) {
        return 15;
    }else if (GroupType(section)) {
        return 10;
    }else{
        return 0;
    }
}
// 每个item之间的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    if (CateType(section)) {
        return floor((CGRectGetWidth(collectionView.frame) - 4*kRecommendItemWidth - 2*kRecommendItemHorEdge)/3);
    }else if (GroupType(section)) {
        return 0;
    }else{
        return 0;
    }
}
// collection设置头和尾视图
- (ASCellNode *)collectionView:(ASCollectionView *)collectionView nodeForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (CateType(indexPath.section)) {
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            LiveHeaderNode *cellNode = [[LiveHeaderNode alloc]initWithDataArray:_liveListModel.lunbo];
            return cellNode;
        }if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
            LiveFooterNode *cellNode = [[LiveFooterNode alloc]initWithItem:_liveListModel.rnotice];
            return cellNode;
        }
    }else if (GroupType(indexPath.section)) {
        ContentHeadNode *cellNode = [[ContentHeadNode alloc]initWithItem];
        return cellNode;
    }
    ASCellNode *cellNode = [[ASCellNode alloc]init];
    return cellNode;
}
// collection设置头视图高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (CateType(section)) {
        if (_index == 0) {
            return CGSizeMake(CGRectGetWidth(self.view.frame), 180);
        }else{
            return CGSizeZero;
        }
    }else if (GroupType(section)) {
        return CGSizeMake(self.view.width, 40);
    }else{
        return CGSizeZero;
    }
}
// collection设置尾视图高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    if (CateType(section)) {
        if (_index == 0) {
            return CGSizeMake(CGRectGetWidth(self.view.frame), 29);
        }else{
            return CGSizeZero;
        }
    }else{
        return CGSizeZero;
    }
}
#pragma mark - data parse 数据解析
// 数据解析
- (NSString *)dataParse:(NSInteger)section
{
    NSString *type;
    if (_liveListModel.cate.count > 0 && _liveListModel.advert.count > 0 && _liveListModel.group.count > 0) {
        if (section == 0) {
            type = @"cate";
        }else if (section == 1) {
            type = @"advert";
        }else{
            type = @"group";
        }
    }else if (_liveListModel.cate.count > 0  && _liveListModel.group.count > 0) {
        if (section == 0) {
            type = @"cate";
        }else{
            type = @"group";
        }
    }else if (_liveListModel.advert.count > 0  && _liveListModel.group.count > 0) {
        if (section == 0) {
            type = @"advert";
        }else{
            type = @"group";
        }
    }else if (_liveListModel.cate.count > 0) {
        type = @"cate";
    }else if (_liveListModel.advert.count > 0) {
        type = @"advert";
    }else if (_liveListModel.group.count > 0) {
        type = @"group";
    }else{
        type = @"";
    }
    return type;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
